package Relays

import (
	"encoding/json"
	"github.com/gorilla/websocket"
	"gopkg.in/go-playground/validator.v9"
	"net"
)

type Relay struct {

}

type NetRelay struct {

}

type Response struct {
	Code int64 `json:"code"`
	Data interface{} `json:"data"`
	Msg string `json:"msg"`
}

func (netRelay *NetRelay) Response(conn net.Conn,code int64,data interface{},msg string){
	var response Response = Response{
		Code: code,
		Data: data,
		Msg:  msg,
	}
	jsonStr,_ := json.Marshal(response)
	conn.Write(jsonStr)
}

func (netRelay *Relay) Response(wsConn *websocket.Conn,code int64,data interface{},msg string){
	//var response Response = Response{
	//	Code: code,
	//	Data: data,
	//	Msg:  msg,
	//}
	//jsonStr,_ := json.Marshal(response)
	//Autowaired.Accessor.WsEvents.SendToSocket(wsConn,string(jsonStr))
}

//. 请求参数验证
func Validate(body interface{}, request interface{}) (err error) {
	err = json.Unmarshal([]byte(body.(string)), &request)
	if err == nil {
		validate := validator.New()
		errValidate := validate.Struct(request)
		if errValidate != nil {
			return errValidate
		}
	}
	return err
}
